from django.utils.html import format_html
from django.conf import settings

def show_link(url_field_name):
    # A function that takes a URL field name as an argument and returns a function that renders the URL field as a link
    def url_link(obj):
        # A function that returns a HTML a tag with the URL field as href and text
        uri = getattr(obj, url_field_name) # example: module/file.pdf
        href = f'/{settings.OSS_FILE_DOWNLOAD_URL}?filepath={uri}'
        filename = uri.split("/")[-1]
        return format_html("<a href='{}' target='_blank'>{}</a>", href, filename)

    url_link.short_description = "download_link" # set the column name
    return url_link